/**
	This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package br.biofoco.p2p.services;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import br.biofoco.p2p.config.ConfigConstants;


public class XMLServiceLoader implements ServiceLoader {
	
	public static final String BASE_DIR = System.getProperty("PEER_HOME", ".");
	private final String filename;

	public XMLServiceLoader(){
		filename = System.getProperty("config.dir", ConfigConstants.CONFIG_DIR) + File.separator + ConfigConstants.SERVICES_FILE;
	}

	public Collection<? extends Service<?>> loadServices() throws IOException {
		
		InputStream input = new FileInputStream(filename);
		Document document = loadXMLDocument(input);
		List<Service<?>> serviceList = populateList(document);
		return serviceList;
	}

	private List<Service<?>> populateList(Document document) {
		List<Service<?>> list = new ArrayList<Service<?>>();
		Element root = document.getRootElement();
		for (Object obj : root.elements("service")){
			Element element = (Element) obj;
			String className = element.getText().trim();
			Long id = Long.parseLong((String)element.attribute("id").getText());
			String servName  = element.attribute("name").getText();
			
			try {
				
				Service<?> service = (Service<?>) Thread.currentThread().getContextClassLoader().loadClass(className).newInstance();
				service.setId(id);
				service.setName(servName);
				service.setStatus(ServiceStatus.AVAILABLE);
				
				list.add(service);
				//service.execute(null);
				
			} catch (InstantiationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
//			catch (ServiceException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
		}
		return list;
	}

	private Document loadXMLDocument(InputStream input) {
		SAXReader reader = new SAXReader();
		try {
			return reader.read(input);
		} catch (DocumentException e) {
			e.printStackTrace();
			return null;
		}
	}
}
